{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "60d1a836f542"
   },
   "outputs": [],
   "source": [
    "# Copyright 2021 The Cirq Developers\n",
    "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "# https://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "ed241e7ca6ee"
   },
   "source": [
    "# Heatmaps"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "3fff35054005"
   },
   "source": [
    "<table class=\"tfo-notebook-buttons\" align=\"left\">\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://quantumai.google/cirq/noise/heatmaps\"><img src=\"https://quantumai.google/site-assets/images/buttons/quantumai_logo_1x.png\" />View on QuantumAI</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://colab.research.google.com/github/quantumlib/Cirq/blob/main/docs/noise/heatmaps.ipynb\"><img src=\"https://quantumai.google/site-assets/images/buttons/colab_logo_1x.png\" />Run in Google Colab</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a target=\"_blank\" href=\"https://github.com/quantumlib/Cirq/blob/main/docs/noise/heatmaps.ipynb\"><img src=\"https://quantumai.google/site-assets/images/buttons/github_logo_1x.png\" />View source on GitHub</a>\n",
    "  </td>\n",
    "  <td>\n",
    "    <a href=\"https://storage.googleapis.com/tensorflow_docs/Cirq/docs/noise/heatmaps.ipynb\"><img src=\"https://quantumai.google/site-assets/images/buttons/download_icon_1x.png\" />Download notebook</a>\n",
    "  </td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "7d4b5cf32fd2"
   },
   "source": [
    "Qubit heatmaps are primarily used for [visualizing calibration metrics](../tutorials/google/visualizing_calibration_metrics.ipynb) but can be used for any custom data. This tutorial shows how you can create a `cirq.Heatmap` for single-qubit data and a `cirq.TwoQubitInteractionHeatmap` for two-qubit data."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "4e8bfd27ed02"
   },
   "outputs": [],
   "source": [
    "try:\n",
    "    import cirq\n",
    "except ImportError:\n",
    "    print(\"installing cirq...\")\n",
    "    !pip install --quiet cirq\n",
    "    print(\"installed cirq.\")\n",
    "    import cirq\n",
    "\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "97b40b3d78a5"
   },
   "source": [
    "# Basic usage\n",
    "Heatmaps maintain an internal state of configuration parameters based on the `kwargs` passed to the constructor, for example: `plot_colorbar=False` to hide the colorbar. For full details on the valid set of parameters, please see the `cirq.Heatmap` reference page.\n",
    "\n",
    "The internal state can also be updated after construction by passing a valid set of `kwargs` to `heatmap.update_config(**kwargs)`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "3f73de4bfaa3"
   },
   "source": [
    "## Single-qubit heatmaps"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "ea9f5f13116a"
   },
   "source": [
    "You can directly create heatmaps using custom data mapping from a grid qubit tuple (single qubit or qubit pair) to a corresponding float value. \n",
    "\n",
    "A single-qubit heatmap example is shown below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "ad8ad19411ef"
   },
   "outputs": [],
   "source": [
    "single_qubit_heatmap = cirq.Heatmap(\n",
    "    {\n",
    "        (cirq.GridQubit(0, 0),): 0.1,\n",
    "        (cirq.GridQubit(0, 1),): 0.2,\n",
    "        (cirq.GridQubit(0, 2),): 0.3,\n",
    "        (cirq.GridQubit(1, 0),): 0.4,\n",
    "    }\n",
    ")\n",
    "\n",
    "_, ax = plt.subplots(figsize=(8, 8))\n",
    "_ = single_qubit_heatmap.plot(ax);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "3ad16f5c35db"
   },
   "source": [
    "## Two-qubit heatmaps"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "46980614cd3c"
   },
   "source": [
    "You can create Two-qubit heatmaps in an analogous manner using tuples of qubit pairs and corresponding (float) data values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "8e612f5d297f"
   },
   "outputs": [],
   "source": [
    "two_qubit_interaction_heatmap = cirq.TwoQubitInteractionHeatmap(\n",
    "    {\n",
    "        (cirq.GridQubit(0, 0), cirq.GridQubit(0, 1)): 0.1,\n",
    "        (cirq.GridQubit(0, 1), cirq.GridQubit(0, 2)): 0.2,\n",
    "        (cirq.GridQubit(1, 0), cirq.GridQubit(0, 0)): 0.3,\n",
    "        (cirq.GridQubit(3, 3), cirq.GridQubit(3, 2)): 0.9,\n",
    "    },\n",
    "    annotation_format=\"0.2f\",\n",
    "    title='Example Two-Qubit Heatmap',\n",
    ")\n",
    "\n",
    "_, ax = plt.subplots(figsize=(8, 8))\n",
    "_ = two_qubit_interaction_heatmap.plot(ax)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "87d966c05772"
   },
   "source": [
    "These types of plots are used for [visualizing two-qubit calibration metrics](../tutorials/google/visualizing_calibration_metrics.ipynb)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "f8f8b4f93ade"
   },
   "source": [
    "# Heatmap configurations\n",
    "You can also pass a valid set of config parameters to the `heatmap.plot(ax, **kwargs)` function, which would temporarily update the internal config based on the parameter values passed as `kwargs`.\n",
    "\n",
    "Below, you can see some examples of useful config parameters for the heatmaps."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "a98c835db769"
   },
   "source": [
    "## Colorbar Properties"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "b53fa792a37a"
   },
   "outputs": [],
   "source": [
    "_, ax = plt.subplots(figsize=(8, 8))\n",
    "_ = two_qubit_interaction_heatmap.plot(\n",
    "    ax,\n",
    "    title='Heatmap with modified Colorbar',\n",
    "    plot_colorbar=True,\n",
    "    colorbar_position='top',\n",
    "    colorbar_size='10%',\n",
    "    colorbar_pad='10%',\n",
    "    colorbar_options={'drawedges': True},\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "9c8b1e8d47c1"
   },
   "source": [
    "## Colormap properties"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "119fbd396d41"
   },
   "outputs": [],
   "source": [
    "_, ax = plt.subplots(figsize=(8, 8))\n",
    "_ = two_qubit_interaction_heatmap.plot(\n",
    "    ax,\n",
    "    title='Heatmap with modified Colormap',\n",
    "    vmin=0.1,\n",
    "    vmax=0.4,\n",
    "    collection_options={'cmap': 'plasma'},\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "459396bf621b"
   },
   "source": [
    "## Annotation text properties"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "de989ba0c37f"
   },
   "outputs": [],
   "source": [
    "_, ax = plt.subplots(figsize=(8, 8))\n",
    "_ = two_qubit_interaction_heatmap.plot(\n",
    "    ax,\n",
    "    title='Heatmap with modified Annotation Text',\n",
    "    annotation_format='.1E',\n",
    "    annotation_text_kwargs={'family': 'monospace', 'size': 'large', 'weight': 'bold'},\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "0cda052504d5"
   },
   "source": [
    "## PolyCollection properties\n",
    "You can modify the properties of the polygons representing the single qubit vertices / two-qubit interaction edges by passing keyword arguments to `collection_options`, which will get forwarded as `**kwargs` to `matplotlib.collections.PolyCollection`.\n",
    "\n",
    "For example: You can specify a color or a list of colors (corresponding to each polygon) as the value for key `edgecolors` to modify the edge colors of the polygons."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "b0ba4868305f"
   },
   "outputs": [],
   "source": [
    "_, ax = plt.subplots(figsize=(8, 8))\n",
    "_ = two_qubit_interaction_heatmap.plot(\n",
    "    ax,\n",
    "    title='Heatmap with modified PolyCollection',\n",
    "    collection_options={'edgecolors': ('red', 'blue', 'blue', 'red'), 'linewidths': 5},\n",
    ")"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "name": "heatmaps.ipynb",
   "toc_visible": true
  },
  "kernelspec": {
   "display_name": "Python 3",
   "name": "python3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
